package gui.Login;


import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import System.ClientConsole;
import System.MainControl;
import control.loginControlNew;
import BaseClasses.User;
import java.awt.SystemColor;

/**login Gui panel*/
public class loginGui extends JPanel implements ActionListener, ItemListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8917178616642730331L;
	/**panel variables*/
	private JTextField Usertxt;
	private JPasswordField passwordField;
	private JTextField textFieldIP;
	private JTextField textFieldPort;
	private JLabel UserNameLbl;
	private JLabel PassLbl;
	private JButton SubButt;
	private JLabel lblIpAddress;
	private JLabel lblPortNumber;
	private JCheckBox consettingCB=null;
	
	private User LogUser;
	private loginControlNew loginC;
	private static String cnfmsg = "Login Successfully!";




	public loginGui ()
	{
		super();
		initialize();

	}

	private void  initialize()
	{
		setBorder(new TitledBorder(null, "Login Screen", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(MainControl.guiX,MainControl.guiY,420,320);
		setLayout(null);

		add(getUnameLable());
		add(getUserText());
		add(getPassLabel());
		add(getPassText());
		add(getPortLbl());
		add(getPortxt());
		add(getIPLbl());
		add(getIPText());
		add(getChBox());
		add(getSubButt());
	}

	public void itemStateChanged(ItemEvent e)
	{
		if (e.getItemSelectable() == consettingCB)
		{
			if (consettingCB.isSelected())
			{
				textFieldIP.setEditable(false);
				textFieldIP.setEnabled(false);
				textFieldPort.setEditable(false);
				textFieldPort.setEnabled(false);
			}
			if (!consettingCB.isSelected())
			{
				textFieldIP.setEditable(true);
				textFieldIP.setEnabled(true);
				textFieldPort.setEditable(true);
				textFieldPort.setEnabled(true);
			}
		}
	}



	private JCheckBox getChBox() 
	{
		consettingCB = new JCheckBox("Use default connection settings");
		consettingCB.setBackground(SystemColor.activeCaptionBorder);
		consettingCB.setFont(new Font("Times New Roman", Font.BOLD, 14));
		consettingCB.setBounds(19, 204, 223, 23);
		consettingCB.setSelected(true);
		consettingCB.addItemListener(this);
		return consettingCB;
	}


	private JTextField getIPText() 
	{
		textFieldIP = new JTextField();
		textFieldIP.setColumns(10);
		textFieldIP.setBounds(204, 157, 150, 25);
		textFieldIP.setEnabled(false);
		textFieldIP.setEditable(false);
		return textFieldIP;
	}

	private JLabel getIPLbl() {
		lblIpAddress = new JLabel("Host:");
		lblIpAddress.setHorizontalAlignment(SwingConstants.CENTER);
		lblIpAddress.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblIpAddress.setBounds(3, 150, 120, 36);
		return lblIpAddress;
	}

	private JTextField getPortxt() {
		textFieldPort = new JTextField();
		textFieldPort.setColumns(10);
		textFieldPort.setBounds(204, 114, 150, 25);
		textFieldPort.setEnabled(false);
		textFieldPort.setEditable(false);
		return textFieldPort;
	}

	private JLabel getPortLbl() {
		lblPortNumber = new JLabel("Port Number:");
		lblPortNumber.setHorizontalAlignment(SwingConstants.CENTER);
		lblPortNumber.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblPortNumber.setBounds(8, 107, 120, 36);
		return lblPortNumber;
	}

	private JLabel getUnameLable() {
		UserNameLbl = new JLabel("Id Number:");
		UserNameLbl.setFont(new Font("Times New Roman", Font.BOLD, 15));
		UserNameLbl.setHorizontalAlignment(SwingConstants.CENTER);
		UserNameLbl.setBounds(3, 24, 120, 36);
		return UserNameLbl;
	}


	private JLabel getPassLabel() {
		PassLbl = new JLabel("Password:");
		PassLbl.setHorizontalAlignment(SwingConstants.CENTER);
		PassLbl.setFont(new Font("Times New Roman", Font.BOLD, 15));
		PassLbl.setBounds(-3, 60, 120, 36);

		return PassLbl;
	}


	private JTextField getPassText() {
		passwordField = new JPasswordField();
		passwordField.setHorizontalAlignment(SwingConstants.LEFT);
		passwordField.setBounds(204, 67, 150, 25);
		return passwordField;
	}

	private JTextField getUserText() {
		Usertxt = new JTextField();
		Usertxt.setBounds(204, 31, 150, 25);

		Usertxt.setColumns(10);
		return Usertxt;
	}

	private JButton getSubButt() {
		SubButt = new JButton("Login"); //,MainControl.Iconlogin
		//SubButt.setIcon(MainControl.Iconlogin);
		SubButt.setSize(MainControl.btnsDimesion);
		SubButt.setLocation(152, 234);
		SubButt.setFont(MainControl.guiFont);
		SubButt.setMnemonic(KeyEvent.VK_ENTER);
		SubButt.addActionListener(this);
		return SubButt;
	}

	private void clearFields()
	{
		Usertxt.setText(null);
		passwordField.setText(null);
		textFieldPort.setText(null);
		textFieldIP.setText(null);
	}


	public void actionPerformed(ActionEvent Ae) 
	{
		if (Ae.getSource() == SubButt)
		{
			if (consettingCB.isSelected())
			{
				LogUser = new User(Usertxt.getText(),passwordField.getPassword());
				loginC = new loginControlNew();
				if (true == loginC.createLogin(ClientConsole.DEFAULT_PORT, ClientConsole.DEDAULT_HOST))
				{
					if (loginC.sendToServer(LogUser) == true)
					{
						JOptionPane.showMessageDialog(null,cnfmsg,"Login Result",JOptionPane.INFORMATION_MESSAGE);
						MainControl.setGUI(MainControl.currentUser.getRole(),this);
					}
					else
					{
						JOptionPane.showMessageDialog(null,loginC.getMsg(),"Login Result",JOptionPane.INFORMATION_MESSAGE);
						if (loginC.getMode() != 2)
							clearFields();
						else
							MainControl.setGUI(MainControl.currentUser.getRole(),this);
					}
				}
				else{
					JOptionPane.showMessageDialog(null,"Error: Can't setup connection to server! Terminating client.\n" +
							"please check the server IP and try again.\n" +
							"for any further help, please contact your administrator.","CCRM Error",JOptionPane.ERROR_MESSAGE);
				}

			}
			else{
				LogUser = new User(Usertxt.getText(),passwordField.getPassword());
				loginC = new loginControlNew();				
				if (loginC.createLogin(Integer.parseInt(textFieldPort.getText()), textFieldIP.getText())==true)
				{
					if (loginC.sendToServer(LogUser) == true)
					{
						JOptionPane.showMessageDialog(null,cnfmsg,"Succeeded",JOptionPane.INFORMATION_MESSAGE);
						MainControl.setGUI(MainControl.currentUser.getRole(),this);
					}
					else
					{
						JOptionPane.showMessageDialog(null,loginC.getMsg(),"Login Result",JOptionPane.INFORMATION_MESSAGE);
						if (loginC.getMode() != 2)
							clearFields();
						else
							MainControl.setGUI(MainControl.currentUser.getRole(),this);
					}
				}
				else{
					JOptionPane.showMessageDialog(null,"Error: Can't setup connection to server! Terminating client.\n" +
							"please check the server IP and try again.\n" +
							"for any further help, please contact your administrator.","CCRM Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		}


	}
}
